using System;
using System.Collections.Generic;
using System.Text;

namespace Coriander.Xmpp
{
    /// <summary>
    /// 
    /// </summary>
    public class State
    {
        StringBuilder response = new StringBuilder();

        Byte[] buffer;

        static readonly Object syncRoot = new Object();

        /// <summary>
        /// Falg indicating whether this instance has had any data written to it.
        /// </summary>
        public Boolean Empty 
        {
            get { return response.Length == 0; }
        }

        /// <summary>
        /// 
        /// </summary>
        public Byte[] Buffer
        {
            get { return buffer; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        public void Append(Byte[] data, Int32 index, Int32 count) 
        {
            lock (syncRoot)
            {
                response.Append(Encoding.ASCII.GetString(data, index, count));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Byte[] GetBytes() 
        {
            lock (syncRoot)
            {
                return Encoding.ASCII.GetBytes(response.ToString());
            }
        }

        /// <summary>
        /// ctor
        /// </summary>
        private State() : this(1024){ }

        /// <summary>
        /// ctor : Int32
        /// </summary>
        /// <param name="bufferLength"></param>
        public State(Int32 bufferLength) 
        {
            this.buffer = new Byte[Math.Abs(bufferLength)];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return response.ToString();
        }
    }
}
